package collision;

import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class Craft {

    private String craft = "/Sprites/pikachu.gif";

    private int dx;
    private int dy;
    private int x;
    private int y;
    private int imagex1;
    private int imagew;
    private int width;
    private int height;
    private boolean visible;
    private Image image;
    private ArrayList missiles;
    private Table table;
    private int MISSILEDELAY = 200;


    public Craft() {
        ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
        image = ii.getImage();
        width = image.getWidth(null);
        height = image.getHeight(null);
        missiles = new ArrayList();
        visible = true;
        imagew = 30;
        imagex1 = 0;
        x = 100;
        y = 440;
        table = new Table();
    }
    
    public Craft(int x, int y) {
        ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
        image = ii.getImage();
        width = image.getWidth(null);
        height = image.getHeight(null);
        missiles = new ArrayList();
        visible = true;
        imagew = 30;
        imagex1 = 0;
        System.out.println("x =" + x +"  y =" + y);
        this.x = x;
        this.y = y;
    }


    public void move() {
        if(imagex1 != 60)
            imagex1 += imagew;
        else imagex1 = 0;
        
        x += dx;
        y += dy;

        if (x < 1) {
            x = 1;
        }

        if (y < 1) {
            y = 1;
        }
    }
    
    public void move(int i) {
        if( i%50 == 0)
            if(imagex1 != 2*imagew)
                imagex1 += imagew;
            else imagex1 = 0;
        
        x += dx;
        y += dy;

        if (x < 1) {
            x = 1;
        }

        if (y < 1) {
            y = 1;
        }

        if(i % MISSILEDELAY == 0){
            fire();
        }
    }

    public int getImagex1(){
    
        return imagex1;
    }
    
    public int getImagew(){
        return imagew;
    }
    
    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Image getImage() {
        return image;
    }

    public ArrayList getMissiles() {
        return missiles;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public boolean isVisible() {
        return visible;
    }

    public Rectangle getBounds() {
        if(visible)
        return new Rectangle(x, y, imagew, 30);
        else return new Rectangle( 0, 0, 0, 0);
    }

    public void keyPressed(KeyEvent e) {

        int key = e.getKeyCode();

        if (key == KeyEvent.VK_SPACE) {
            fire();
        }

        if (key == KeyEvent.VK_LEFT) {
            dx = -1;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 1;
        }

        if (key == KeyEvent.VK_UP) {
            dy = -1;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 1;
        }
        
    }

    public void fire() {
        missiles.add(new Missile(x + imagew, y +30/2));
    }

    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }
    
    public void mouseClicked(MouseEvent e){
       x = table.insertX(e.getX() - imagew/2);
       y = table.insertY(e.getY() - height/2);
       visible = table.isVisible(e.getX() - imagew/2, e.getY() - height/2);
       System.out.printf("\n x = %d, y = %d", x, y);
       System.out.printf("\n Coluna: %d", table.findCol(x));
       System.out.printf("\n Linha: %d", table.findRow(y));
    }
    
}